package com.cy.pj.common.pojo;

import com.cy.pj.common.util.PageUtil;
import com.github.pagehelper.ISelect;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 通过此对象封装服务端响应到客户端的数据,让数据以一种规范化的格式呈现给客户端.
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JsonResult implements Serializable {
    private static final long serialVersionUID = -8416999444660048022L;
    /**状态码*/
    private Integer state=1;//1 表示OK,0表示Error
    /**状态码信息*/
    private String message="ok";
    /**封装正确的查询结果*/
    private Object data;

    public JsonResult(String message){
        this.message=message;
    }
    public JsonResult(Integer state, String message){
        this(message);
        this.state=state;
    }
    public JsonResult(Object data){//new JsonResult(list)
        this.data=data;
    }
    //当出现异常时,可以通过此构造方法对异常信息进行封装
    public JsonResult(Throwable e){//new JsonResult(exception);
        this(0,e.getMessage());
    }

    public JsonResult(ISelect select){
        this.data = PageUtil.startPage().doSelectPageInfo(select);
    }
}
